Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[evm] refactor evm functions parameters #3959

Merged
merged 5 commits into from
Nov 14, 2023

Conversation

envestcc
Copy link
Member

@envestcc envestcc commented Nov 1, 2023

Description

Introducing HelperContext in EVM simplifies the parameter passing in public methods, mitigates the issue of passing parameters through multiple layers, and minimize subsequent modifications when adding parameters.

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • [] New feature (non-breaking change which adds functionality)
  • Code refactor or improvement
  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)
  • [] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • [] My code follows the style guidelines of this project
  • [] I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [] My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • [] New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

Copy link

codecov bot commented Nov 1, 2023

Codecov Report

Merging #3959 (4ed4158) into master (e1f0636) will increase coverage by 0.74%.
Report is 100 commits behind head on master.
The diff coverage is 77.22%.

❗ Current head 4ed4158 differs from pull request most recent head 8d71e84. Consider uploading reports for the commit 8d71e84 to get more accurate results

@@            Coverage Diff             @@
##           master    #3959      +/-   ##
==========================================
+ Coverage   75.38%   76.12%   +0.74%     
==========================================
  Files         303      330      +27     
  Lines       25923    28076    +2153     
==========================================
+ Hits        19541    21372    +1831     
- Misses       5360     5606     +246     
- Partials     1022     1098      +76     
Files Coverage Δ
action/candidate_register.go 90.00% <100.00%> (ø)
action/candidate_update.go 89.01% <100.00%> (+0.12%) ⬆️
action/claimreward.go 90.62% <100.00%> (-0.42%) ⬇️
action/depositreward.go 87.50% <100.00%> (-0.56%) ⬇️
action/protocol/context.go 67.66% <100.00%> (+0.49%) ⬆️
action/protocol/execution/protocol.go 48.83% <100.00%> (+6.73%) ⬆️
action/protocol/rewarding/protocol.go 80.08% <ø> (+0.67%) ⬆️
action/protocol/rewarding/reward.go 90.07% <100.00%> (+0.60%) ⬆️
...tion/protocol/staking/candidate_buckets_indexer.go 89.31% <100.00%> (-0.47%) ⬇️
...col/staking/ethabi/stake_composite_bucketscount.go 100.00% <100.00%> (ø)
... and 71 more

... and 4 files with indirect coverage changes

📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today!

@envestcc envestcc force-pushed the pr_evmfunction branch 2 times, most recently from b8b6ed6 to 59c3d73 Compare November 7, 2023 01:24
@envestcc envestcc marked this pull request as ready for review November 7, 2023 01:25
Comment on lines +641 to +650
helperCtx := mustGetHelperCtx(ctx)
ctx = WithHelperCtx(ctx, HelperContext{
GetBlockHash: helperCtx.GetBlockHash,
DepositGasFunc: func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) {
return nil, nil
},
Sgd: nil,
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add TODO: move the logic out of SimulateExecution

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is preferred to be done in this pr since putting mustGetHelperCtx aside WithHelperCtx is strange

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will open another PR to address it, to avoid causing too many code conflicts in the upcoming PRs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add TODO

Comment on lines -646 to -649
func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) {
return nil, nil
},
nil,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong implementation. SimulateExecution is supposed to have the same behavior as ExecuteContract, including gas

Comment on lines -143 to -149
func(height uint64) (hash.Hash256, error) {
return hash.ZeroHash256, nil
},
func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) {
return nil, nil
},
nil,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a TODO

func (core *coreService) simulateExecution(ctx context.Context, addr address.Address, exec *action.Execution, getBlockHash evm.GetBlockHash) ([]byte, *action.Receipt, error) {
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
GetBlockHash: getBlockHash,
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a TODO to add depositGas

data, _, err := factory.SimulateExecution(ctx, addr, ex, dao.GetBlockHash)
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
GetBlockHash: dao.GetBlockHash,
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

if err != nil {
return nil, nil, err
}
sgd := ps.helperCtx.Sgd
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change L277-287:

	receiver, sharedGas, err = processSGD(ctx, sm, execution, consumedGas, helperCtx)
	if err != nil {
		return nil, nil, errors.Wrap(err, "failed to process Sharing of Gas-fee with DApps")
	}

after modifying func processSGD(ctx, sm, execution, consumeGas, helperCtx)

Comment on lines 303 to 322
func depositeGas(ctx context.Context, sm protocol.StateManager, execution *action.Execution, consumedGas uint64, gasPrice *big.Int, hCtx HelperContext, fCtx protocol.FeatureCtx) (*action.TransactionLog, error) {
var (
receiver address.Address
sharedGas uint64
sharedGasFee, totalGasFee *big.Int
err error
)
if fCtx.SharedGasWithDapp && hCtx.Sgd != nil {
receiver, sharedGas, err = processSGD(ctx, sm, execution, consumedGas, hCtx.Sgd)
if err != nil {
return nil, errors.Wrap(err, "failed to process Sharing of Gas-fee with DApps")
}
}
if sharedGas > 0 {
sharedGasFee = big.NewInt(int64(sharedGas))
sharedGasFee.Mul(sharedGasFee, gasPrice)
}
totalGasFee = new(big.Int).Mul(new(big.Int).SetUint64(consumedGas), gasPrice)
return hCtx.DepositGasFunc(ctx, sm, receiver, totalGasFee, sharedGasFee)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not what I mean.

func processSGD(ctx context.Context, sm protocol.StateManager, execution *action.Execution, consumedGas uint64, sgd SGDRegistry,
) (address.Address, uint64, error) {
    if sgd == nil {
        return nil, 0, nil
    }
    ....

thus, outside, we only need to checkif ps.featureCtx.SharedGasWithDapp

Copy link
Member Author

@envestcc envestcc Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted, and added TODO

@@ -291,7 +294,10 @@ func getContractReaderForGenesisStates(ctx context.Context, sm protocol.StateMan
return nil, err
}

res, _, err := evm.SimulateExecution(ctx, sm, addr, ex, getBlockHash)
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WithHelperCtx could be called by the caller, then getBlockHash argument can be removed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a TODO

@@ -316,6 +300,27 @@ func ExecuteContract(
return retval, receipt, nil
}

func depositeGas(ctx context.Context, sm protocol.StateManager, execution *action.Execution, consumedGas uint64, gasPrice *big.Int, hCtx HelperContext, fCtx protocol.FeatureCtx) (*action.TransactionLog, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change isn't related to the intro of WithHelperCtx, which could be done in another pr.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted

Copy link

sonarcloud bot commented Nov 13, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 6 Code Smells

No Coverage information No Coverage information
7.8% 7.8% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

@envestcc envestcc merged commit be3a09a into iotexproject:master Nov 14, 2023
3 of 4 checks passed
@envestcc envestcc deleted the pr_evmfunction branch November 14, 2023 09:27
@envestcc envestcc linked an issue Nov 17, 2023 that may be closed by this pull request
6 tasks
@envestcc envestcc mentioned this pull request Nov 24, 2023
6 tasks
@envestcc envestcc removed a link to an issue Nov 24, 2023
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants